package com.example.zhu95.mytimber.adapters;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.zhu95.mytimber.MyApplication;
import com.example.zhu95.mytimber.R;
import com.example.zhu95.mytimber.activity.BaseActivity;
import com.example.zhu95.mytimber.entity.AlbumBean;
import com.example.zhu95.mytimber.entity.SongBean;
import com.example.zhu95.mytimber.entity.SongData;
import com.example.zhu95.mytimber.listener.PopupMenuItemClickListener;
import com.example.zhu95.mytimber.ui.MusicLinearClip;
import com.example.zhu95.mytimber.utils.Util;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.zhu95.mytimber.ConstantValue.MusicState_Paused;
import static com.example.zhu95.mytimber.ConstantValue.MusicState_Playing;
import static com.example.zhu95.mytimber.ConstantValue.PlayMusicCmd_Play;
import static com.example.zhu95.mytimber.activity.BaseActivity.state;

/**
 * RecyclerView  (viewpager_library_songs.xml.xml)的子项(reycclerview_item_vpopup.xmll)适配器
 * 用于带popupmenu但是无拖动排序的RecyclerView
 * 1、Library->Songs
 *
 * @author zhu95 on 2017/11/29
 */

public class ItemVpopupAdapter extends RecyclerView.Adapter<ItemVpopupAdapter.ViewHolder> implements SectionIndexer {
    private List<SongBean> songs;
    private ItemType itemType = ItemType.SONG;
    private BaseActivity activity;

    /**
     * 适用于歌曲，专辑，艺术家的不同类型的显示方式
     */
    public enum ItemType {
        SONG,
        ARTIST,
        ALBUM
    }

    public ItemVpopupAdapter(List<SongBean> songs, BaseActivity activity) {
        this.songs = songs;
        this.activity = activity;
    }

    public ItemVpopupAdapter(List<SongBean> songs, ItemType itemType, BaseActivity activity) {
        this.songs = songs;
        this.itemType = itemType;
        this.activity = activity;
    }

    public List<SongBean> getSongs() {
        return songs;
    }

    public void setSongs(List<SongBean> songs) {
        if (this.songs != songs) {
            this.songs.clear();
            this.songs.addAll(songs);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_vpopup, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final SongBean song = songs.get(position);
        String albumArt = song.getAlbumArt();
        if (!TextUtils.isEmpty(albumArt)) {
            Glide.with(holder.itemView.getContext()).load(albumArt).centerCrop().into(holder.imageArtist);
        } else {
            Glide.with(holder.itemView.getContext()).load(R.drawable.ic_empty_music2).centerCrop().into(holder.imageArtist);
        }
        holder.songMoreVert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu = new PopupMenu(v.getContext(), v);
                popupMenu.getMenuInflater().inflate(R.menu.song_more_popup, popupMenu.getMenu());
                popupMenu.setOnMenuItemClickListener(
                        new PopupMenuItemClickListener(activity, song.getId(), Util.INSTANCE.getIdsStrFormat(songs), ItemVpopupAdapter.this)
                );
                popupMenu.show();
            }
        });

        holder.tvSong.setText(song.getTitle());
        if (itemType == ItemType.SONG) {
            //设置播放时的字体颜色和播放的
            if (song.getId() == BaseActivity.songid) {
                holder.tvSong.setTextColor(MyApplication.getContext().getResources().getColor(R.color.colorAccent));
                if (TextUtils.equals(state, MusicState_Playing)) {
                    holder.musicLinearClip.setVisibility(View.VISIBLE);
                } else if (TextUtils.equals(state, MusicState_Paused)) {
                    holder.musicLinearClip.setVisibility(View.GONE);
                }
            } else {
                holder.tvSong.setTextColor(Color.BLACK);
                holder.musicLinearClip.setVisibility(View.GONE);
            }
            holder.tvItemInfo.setText(song.getArtist());
        } else if (itemType == ItemType.ALBUM) {
            holder.tvItemInfo.setText(song.getPlayTime());
        } else if (itemType == ItemType.ARTIST) {
            AlbumBean albumBean = SongData.findAlbumByID(song.getAlbumId());
            holder.tvItemInfo.setText(albumBean.getAlbum());
        }
        //点击事件
        holder.songItemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.sendPlayMusicCmdBroadcast(song.getId(), Util.INSTANCE.getIdsStrFormat(songs), PlayMusicCmd_Play);
            }
        });
    }

    @Override
    public int getItemCount() {
        return songs.size();
    }

    @Override
    public Object[] getSections() {
       /* Object[] objects = new Object[getItemCount()];
        for (int i = 0, length = getItemCount(); i < length; i++) {
            objects[i] = songs.get(i);
        }
        */
        return songs.toArray();
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        return sectionIndex;
    }

    @Override
    public int getSectionForPosition(int position) {
        if (position >= songs.size()) {
            position = songs.size() - 1;
        }
        return position;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_artist)
        ImageView imageArtist;
        @BindView(R.id.tv_song)
        TextView tvSong;
        @BindView(R.id.tv_itemInfo)
        TextView tvItemInfo;
        @BindView(R.id.more_vert)
        ImageView songMoreVert;
        @BindView(R.id.play_sign)
        MusicLinearClip musicLinearClip;
        @BindView(R.id.song_item_layout)
        RelativeLayout songItemLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
